package com.kaver.feign.rpc.eureka;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: EurekaAppStatusScheduler
 * @Description: eureka应用状态同步
 * @author kaver
 * @date 2019年5月29日 下午4:03:28
 * 
 */
public class EurekaAppStatusScheduler {

	private static ScheduledExecutorService service = Executors.newScheduledThreadPool(1);

	public static void start() {
		service.scheduleAtFixedRate(new Runnable() {

			@Override
			public void run() {
				synStatus();
			}
		}, 30, 30, TimeUnit.SECONDS);
	}

	public static void synStatus() {
		try {
			Map<String, Map<String, EurekaAppInstance>> cache = EurekaAppCache.getCache();
			Set<String> set = cache.keySet();
			if (!set.isEmpty()) {
				for (String appName : set) {
					cache.put(appName, EurekaApi.getAppInstance(EurekaServiceFactory.getEurekaUrl(), appName));
				}
			}
		} catch (Exception e) {

		}

	}

}
